package com.pomr.util.common;

import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.chainsaw.Main;

public class IpMessage {
	
	public static String getDateOf16(Date date){
		String dateStr = "";
		String sum2 = "";
		SimpleDateFormat sdf1 = new SimpleDateFormat("yy-MM-dd");
		SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
		String ymd = sdf1.format(date);
		String[] ymdarr = ymd.split("-");
		//年月日
		for(int i=0; i<ymdarr.length; i++){
			Integer datechild = Integer.valueOf(ymdarr[i]);
			String cdStr = "00"+Integer.toHexString(datechild).toUpperCase();
			String dstr = cdStr.substring(cdStr.length()-2);//获取16进制
			if(i == 0){
				sum2 = dstr;
			}else{
				sum2 = xor(sum2, dstr);
			}
			dateStr += dstr;
		}
		//时分
		String hm = sdf2.format(date);
		String[] hmarr = hm.split(":");
		for(int i=0; i<hmarr.length; i++){
			Integer datechild = Integer.valueOf(hmarr[i]);
			String cdStr = "00"+Integer.toHexString(datechild).toUpperCase();
			String dstr = cdStr.substring(cdStr.length()-2);//获取16进制
			sum2 = xor(sum2, dstr);
			dateStr += dstr;
		}
		sum2 += ("00"+sum2); 
		return dateStr+sum2.substring(sum2.length()-2);
	}

	public static String getIpOf16(HttpServletRequest request) {
		try {
		String ipStr = "";
		InetAddress address = InetAddress.getLocalHost();//获取的是本地的IP地址 //PC-20140317PXKX/192.168.0.121
		String ip = address.getHostAddress();//192.168.0.121  
//		String ip = getIpAddress(request);
		ip = "42.121.236.135";
		System.out.println(ip);
		String sum1 = "";
		String[] ips = ip.split("\\.");
		for(int i=0;i<ips.length;i++){
			Integer ipchild = Integer.valueOf(ips[i]);
			String cipStr = "00"+Integer.toHexString(ipchild).toUpperCase();
			String ipstr = cipStr.substring(cipStr.length()-2);
			
			if(i == 0){
				sum1 = ipstr;
			}else{
				sum1 = xor(sum1, ipstr);
			}
			ipStr += ipstr;
		}
		int port = request.getServerPort();
		port = 8014;
		System.out.println(port);
		String portStr = Integer.toHexString(port).toUpperCase();
		portStr = portStr.substring(portStr.length()-2)+portStr.substring(0, 2);
		sum1 = xor(sum1, portStr.substring(0, 2));
		sum1 = xor(sum1, portStr.substring(portStr.length()-2));
		sum1 += ("00"+sum1); 
		
		return ipStr+portStr+sum1.substring(sum1.length()-2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
	
	public static String xor(String strHex_X,String strHex_Y){
		//将x、y转成二进制形式   
        String anotherBinary=Integer.toBinaryString(Integer.valueOf(strHex_X,16));   
        String thisBinary=Integer.toBinaryString(Integer.valueOf(strHex_Y,16));   
        String result = "";   
        //判断是否为8位二进制，否则左补零   
        if(anotherBinary.length() != 8){   
        for (int i = anotherBinary.length(); i <8; i++) {   
                anotherBinary = "0"+anotherBinary;   
            }   
        }   
        if(thisBinary.length() != 8){   
        for (int i = thisBinary.length(); i <8; i++) {   
                thisBinary = "0"+thisBinary;   
            }   
        }   
        //异或运算   
        for(int i=0;i<anotherBinary.length();i++){   
        //如果相同位置数相同，则补0，否则补1   
                if(thisBinary.charAt(i)==anotherBinary.charAt(i))   
                    result+="0";   
                else{   
                    result+="1";   
                }   
            }  
        return Integer.toHexString(Integer.parseInt(result, 2)).toUpperCase();
	}
	
	/**
	 * 获取检校码
	 * @param str
	 * @return
	 */
	public static String getSum(String str){
		int sum = 0; 
        char[] chars = str.substring(0, 39).toCharArray();   
        for (int i = 0; i < chars.length; i++) {  
        	sum += (int)chars[i];
        }
        String s = "0000"+Integer.toHexString(sum).toUpperCase();
		return s.substring(s.length()-4);
	}
	
	public static void main(String[] args) {
		System.out.println(IpMessage.getIpOf16(null));
	}

}
